﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace MassIntellect.WorkIt
{
    /// <summary>
    /// Селектор: Имя канала = имя элемента
    /// </summary>
    public class XNamePipeSelector : IXmlPipeSelector
    {
        [Serialize(SerializeAs.Attribute, IgnoreDefault = true)]
        public bool IncludeNamespace { get; set; }

        private Dictionary<string, IPipe> pipes = new Dictionary<string, IPipe>();

        #region IXmlPipeSelector Members

        public IWorkFrame Owner { get; set; }

        public IEnumerable<IPipe> Pipes
        {
            get { return pipes.Values; }
        }

        public IPipe this[string NameIndex]
        {
            get
            {
                IPipe pipe;
                if (!pipes.TryGetValue(NameIndex, out pipe))
                    pipes.Add(NameIndex,
                        pipe = new Pipe { Scope = Scope.Public, Name = NameIndex });
                return pipe;
            }
        }

        public IPipe Select(XElement xe)
        {
            return IncludeNamespace ? this[xe.Name.ToString()] : this[xe.Name.LocalName];
        }

        #endregion
    }
}
